package com.kk.app.render;

import static android.opengl.EGL14.EGL_CONTEXT_CLIENT_VERSION;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;


import com.kk.app.utils.EglEnv;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;

/**
 * @author wk
 * @date 2024/12/5 16:39
 * @describe
 */
public class MVRenderSurfaceView extends GLSurfaceView {

    private SurfaceViewRender mRenderer = null;

    public MVRenderSurfaceView(Context context) {
        this(context, null);
    }

    public MVRenderSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        setZOrderOnTop(true);
//        setZOrderMediaOverlay(true);
        setEGLContextClientVersion(2);
        setZOrderOnTop(true);
        setZOrderMediaOverlay(true);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        this.setEGLContextFactory(new EGLContextFactory() {

            @Override
            public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
                int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};

                return egl.eglCreateContext(display, eglConfig, EglEnv.getSharedEGLContext(), attrib_list);
            }

            @Override
            public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {

                if (!egl.eglDestroyContext(display, context)) {
                    Log.e("DefaultContextFactory", "display:" + display + " context: " + context);
                }
            }
        });

        mRenderer = new SurfaceViewRender();
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    /**
     * 设置Surface
     * @param surfaceRenderListener
     */
    public void setSurfaceRenderListener(ISurfaceRenderListener surfaceRenderListener) {
        if (mRenderer != null) {
            mRenderer.setSurfaceRenderListener(surfaceRenderListener);
        }

    }
}
